package org.whatever.aha.zjut.platform.controller.school;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.whatever.aha.zjut.base.constant.AuthConst;
import org.whatever.aha.zjut.base.constant.ErrorCode;
import org.whatever.aha.zjut.base.constant.StatusConstant;
import org.whatever.aha.zjut.base.dto.AjaxResult;
import org.whatever.aha.zjut.base.exception.AppException;
import org.whatever.aha.zjut.platform.annotation.StageLimit;
import org.whatever.aha.zjut.platform.service.competition.CompetitionStageService;
import org.whatever.aha.zjut.platform.service.project.ProjectService;
import org.whatever.aha.zjut.platform.service.school.ProjectScoreService;
import org.whatever.aha.zjut.platform.vo.academy.HistoryVo;
import org.whatever.aha.zjut.platform.vo.school.AcademyCompetitionRoughInfoVo;
import org.whatever.aha.zjut.platform.vo.school.DirectProvinceInfoVo;
import org.whatever.aha.zjut.platform.vo.school.SchoolRecInfoVo;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author Vc
 * @version 1.0
 * @Desc
 * @date 2021/11/22 18:09
 */
@Api(tags = "评委评审结果发布")
@RequestMapping("/v1/school/judgeResultPublic")
@RequiredArgsConstructor
@RestController
@Validated
@SaCheckRole(value = {AuthConst.R_supper, AuthConst.R_school}, mode = SaMode.OR)
public class JudgeResultPublicController {
    final ProjectScoreService projectScoreService;
    final ProjectService projectService;
    final CompetitionStageService competitionStageService;

    @ApiOperation("根据比赛id、阶段id获得分页评审信息")
    @GetMapping("/getJudgeResultPageable/{compId}/{stageId}")
    public AjaxResult<Map> getJudgeResultPageable(@PathVariable("compId")int compId,
                                                  @PathVariable("stageId")int stageId,
                                                  @RequestParam(value = "pageNum", required = true, defaultValue = "1") int pageNum,
                                                  @RequestParam(value = "pageSize", required = true, defaultValue = "10") int pageSize,
                                                  @RequestParam(value = "academyThirdPrize", required = false) Boolean academyThirdPrize) {
        return AjaxResult.SUCCESS(projectScoreService.getJudgeResultPageable(compId, stageId, pageNum, pageSize, academyThirdPrize));
    }

    /**
     * 公示阶段可以修改且此阶段（初赛复赛）未完结
     *
     * @param projectId
     * @param stageId
     * @return
     */
    @StageLimit({"public"})
    @ApiOperation("修改总分")
    @PutMapping("/modifyAvgScore/{projectId}/{stageId}")
    public AjaxResult<Integer> modifyAvgScore(@PathVariable("projectId")int projectId,
                                              @PathVariable("stageId")int stageId,
                                              @RequestParam(value = "score", required = true) BigDecimal score) {
        if(competitionStageService.getStageVoById(stageId).getStageStatus() == StatusConstant.COMPETITION_STAGE_STATUS_COMPLETED.getCode()){
            throw new AppException(ErrorCode.TIME_CHECK_FAILED);
        }
        return AjaxResult.SUCCESS(projectService.modifyAvgScore(projectId, score));
    }

    @ApiOperation("根据比赛年份与赛事类型获取阶段赛事结果")
    @GetMapping("/getHistory")
    AjaxResult<List<HistoryVo>> getHistory(@RequestParam(value = "compYear", required = true) int compYear,
                                           @RequestParam(value = "compType", required = true) int compType,
                                           @RequestParam(value = "stageId", required = true) int stageId){
        return AjaxResult.SUCCESS(projectService.getHistory(compYear, compType, null, stageId));
    }

}
